<?php
require_once 'config.php'; 
require_once 'Tools.class.php'; 
require_once 'HTML.class.php'; 

class User{
	//绑定检查
	public static function verBinding($from,$keyword){
        $sql = "select * from `smartdzu_binding` where openid='$from' limit 1";
        $res = mysql_query($sql);
        if($row=mysql_fetch_array($res)){
        	if($row['enable']=='1'){
        		$str= 'ok';
        	}else{
        		$str="您已取消关注,无法继续使用,请联系管理员!";
        	}	
        }else{
            //未绑定学号操作
            $keyword = trim($keyword);
            if(strpos($keyword,"#")){
                $info = explode("#",$keyword);
                if(strlen($info[0])==12 && strlen($info[1])==6){//信息完整,开始验证
					$code = Tools::getHttp(DZU_URL."stuinfo.asp?xh=".$info[0]);
					if(strlen(trim($code))>10){//检测是否获取到数据
						User::saveUserInfo($code);//存储用户信息
						
						$sfzh = strtoupper(Tools::getInfo($code,"SFZH"));//统一转换为大写
						$shenfen = strtoupper($info[1]);
						
						if((strpos($sfzh,$shenfen)==12) && (strlen($sfzh)==18)){//验证身份证号
							$sql = "INSERT INTO smartdzu_binding (`xuehao`, `openid`) VALUES ('".$info[0]."', '$from')";
	        				mysql_query($sql); 
	                        $str.="绑定成功\n";
							$str.="您的基本信息如下：";
							$str.="\n-----------------";
							$str.="\n姓名：".Tools::getInfo($code,"XM");
							$str.="\n学号：".Tools::getInfo($code,"XH");
							$str.="\n班级：".Tools::getInfo($code,"XZB");
							$str.="\n专业：".Tools::getInfo($code,"ZYMC");
							$str.="\n学院：".Tools::getInfo($code,"XY");
							$str.="\n身份证号：".Tools::getInfo($code,"SFZH");
							$str.="\n-----------------\n<a href=\"".WEB."\">回复\"0\"或\"菜单\"查看菜单</a>";
							
							$msg="新用户信息绑定";
							$msg.="\n-----------------";
							$msg.="\n姓名：".Tools::getInfo($code,"XM");
							$msg.="\n学号：".Tools::getInfo($code,"XH");
							$msg.="\n班级：".Tools::getInfo($code,"XZB");
							$msg.="\n学院：".Tools::getInfo($code,"XY");
							Tools::SendMMS("18315911332",$msg);//短信通知管理员
						}else{
							$str="身份证号不匹配";
						}
					}else{
						 $str ="无法获取到您的信息，请检查您的学号。";	
					}
                }else{
                    $str = "您发送的信息不完整，请发送您的学号(12位)和身份证号(后6位)以#号分割，如：\n201211000000#888888";
                }
            }else{
                $str = "未绑定学号！请发送12位学号和身份证号后6位(以#号分割)完成绑定，如：\n201211000000#888888";
            }
        }
		return $str;
    }
	
	//保存用户信息
	public static function saveUserInfo($info){
		if(strlen(trim($info))>10){
			$xh=Tools::getInfo($info,"XH");
			$sql="select count(*) from smartdzu_user_wx where `xh`='$xh'";
			$result=mysql_query($sql);
			if($result){
				$row = mysql_fetch_array($result);
				if($row[0]==0){
					$xh=Tools::getInfo($info,"XH");
					$xm=Tools::getInfo($info,"XM");
					$xb=Tools::getInfo($info,"XB");
					$csrq=Tools::getInfo($info,"CSRQ");
					$zzmm=Tools::getInfo($info,"ZZMM");
					$mz=Tools::getInfo($info,"MZ");
					$jg=Tools::getInfo($info,"JG");
					$xy=Tools::getInfo($info,"XY");
					//$xi=Tools::getInfo($info,"XI");
					$zymc=Tools::getInfo($info,"ZYMC");
					$xzb=Tools::getInfo($info,"XZB");
					$xz=Tools::getInfo($info,"XZH");
					$dqszj=Tools::getInfo($info,"DQSZJ");
					$rxrq=Tools::getInfo($info,"RXRQ");
					$sfzh=Tools::getInfo($info,"SFZH");
					$mm=Tools::getInfo($info,"MM");$mm=str_replace("'", "", $mm);$mm=str_replace("`", "", $mm);$mm=str_replace(",", "", $mm);
					$rxzf=Tools::getInfo($info,"RXZF");
					$cc=Tools::getInfo($info,"CC");
					$byrq=Tools::getInfo($info,"BYRQ");
					$xxxs=Tools::getInfo($info,"XXXS");
					$xykh=Tools::getInfo($info,"XYKH");
					$xssj=Tools::getInfo($info,"XSSJ");
					$yhkh=Tools::getInfo($info,"YHKH");
					$first_login=date("Y-m-d H:i:s");
					$last_login=date("Y-m-d H:i:s");
					if($xh!="null"){
						$sql="INSERT INTO `smartdzu_user_wx`(`xh`, `xm`, `xb`, `csrq`, `zzmm`, `mz`, `jg`, `xy`, `yhkh`, `zymc`, `xzb`, `xz`, `dqszj`, `rxrq`, `sfzh`, `mm`, `rxzf`, `cc`, `byrq`, `xxxs`, `first_login`, `last_login`,`xykh`,`xssj`) VALUES ('$xh', '$xm', '$xb', '$csrq', '$zzmm', '$mz', '$jg', '$xy', '$yhkh', '$zymc', '$xzb', '$xz', '$dqszj', '$rxrq', '$sfzh', '$mm', '$rxzf', '$cc', '$byrq', '$xxxs', '$first_login', '$last_login','$xykh','$xssj')";
						mysql_query($sql);
					}
				}	
			}
		}
	}

	//根据openid获取学号
    public static function getXH($openid){
    	$sql = "select xuehao from `smartdzu_binding` where openid='$openid'";
        $res = mysql_query($sql);
        $row = mysql_fetch_array($res);
        return trim($row[0]);
    }

    //根据openid获取真是姓名
    public static function getXM($openid){
        $sql = "select `xm` from `smartdzu_user_wx` where `xh`=(select `xuehao` from `smartdzu_binding` where `openid`='$openid' limit 1)";
        $res = mysql_query($sql);
        $row = mysql_fetch_array($res);
        return trim($row[0]);
    }

    //根据openid获取校园卡号
    public static function getXYKH($openid){
        $sql = "select `xykh` from `smartdzu_user_wx` where `xh`=(select `xuehao` from `smartdzu_binding` where `openid`='$openid' limit 1)";
        $res = mysql_query($sql);
        $row = mysql_fetch_array($res);
        return trim($row[0]);
    }

	//根据学号或openid使user无效
    public static function setDisable($xh){
    	$sql="UPDATE `smartdzu_binding` SET `enable` = '0' WHERE `xuehao` ='$xh' OR `openid` = '$xh';";
    	mysql_query($sql);
    }
}
?>